package com.jeeplus.modules.common.excel.common.validator.impl;

import com.jeeplus.common.utils.StringUtil;
import com.jeeplus.modules.api.enrollmentclass.entity.EnrollmentClass;
import com.jeeplus.modules.api.enrollmentclass.service.EnrollmentClassService;
import com.jeeplus.modules.api.major.entity.Major;
import com.jeeplus.modules.api.major.service.MajorService;
import com.jeeplus.modules.common.excel.common.anno.ClassName;
import com.jeeplus.modules.common.excel.common.validator.abs.AbstractValidator;
import com.jeeplus.modules.common.utils.SpringBeanUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 班级名称校验
 * @author wang.yishu
 */
public class ClassNameValidator extends AbstractValidator {

    private ClassName className;

    private static final String CLASS_KEY = "className";

    private static final String MAJOR_KEY = "majorName";

    @Override
    public boolean canValidate(Field field) {
        this.className =field.getDeclaredAnnotation(ClassName.class);
        return this.className != null;
    }

    @Override
    public String toValidate(Object entity, Field field) {
        Map<String, List<Object>> necessaryValidateData = this.chain.getNecessaryValidateData();
        List<Object> classData = null;
        List<Object> majorData = null;
        if (!necessaryValidateData.containsKey(CLASS_KEY)) {
            EnrollmentClassService enrollmentClassService = SpringBeanUtils.getBean(EnrollmentClassService.class);
            classData = new ArrayList<>();
            EnrollmentClass enrollmentClassParam = new EnrollmentClass();
            classData = Collections.singletonList(enrollmentClassService.findAllClass(enrollmentClassParam));
            necessaryValidateData.put(CLASS_KEY, classData);
        } else {
            classData = necessaryValidateData.get(CLASS_KEY);
        }
        if (!necessaryValidateData.containsKey(MAJOR_KEY)) {
            MajorService majorService = SpringBeanUtils.getBean(MajorService.class);
            majorData = new ArrayList<>();
            Major majorParam = new Major();
            majorParam.setGradation("1,2,3,4,5");//层次：1本科,4专升本,5第二学位:
            List<Major> majorList = majorService.findList(majorParam);
            if (majorList != null && majorList.size() > 0) majorData.addAll(majorList);
            necessaryValidateData.put(MAJOR_KEY, majorData);
        } else {
            majorData = necessaryValidateData.get(MAJOR_KEY);
        }
        String transformTo = className.transformTo();
        field.setAccessible(true);
        try {
            Object value = field.get(entity);
            if (value == null) return null;
            if (value instanceof String) {
                String classNameStr = value.toString();
                String forMajorStr = className.forMajor();
                EnrollmentClass enrollmentClass = null;
                Major major = null;
                String majorId = null;
                if (StringUtil.isNotBlank(forMajorStr)) {
                    Field forMajorField = entity.getClass().getDeclaredField(forMajorStr);
                    String majorName = (String) forMajorField.get(entity);
                    List<Object> majorList = majorData.stream().filter(obj -> ((Major) obj).getMajorName().equals(majorName)).collect(Collectors.toList());
                    if (majorList.size() > 0) {
                        major = (Major) majorList.get(0);
                        majorId = major.getId();
                    }
                }

                String finalMajorId = majorId;
                List<Object> classList = classData.stream().filter(obj -> !((EnrollmentClass) obj).getClassName().equals(classNameStr) ||
                        !StringUtil.isNotBlank(finalMajorId) || ((EnrollmentClass) obj).getMajor().equals(finalMajorId)).collect(Collectors.toList());
                if (classList.size() == 0) {
                    return className.msg();
                } else {
                    enrollmentClass = (EnrollmentClass) classList.get(0);
                }

                if (StringUtil.isNotBlank(transformTo)) {
                    Field transformToField = entity.getClass().getDeclaredField(transformTo);
                    transformToField.setAccessible(true);
                    transformToField.set(entity, enrollmentClass.getId());
                }
            } else {
                return "班级名称数据类型错误！";
            }
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }
}
